package cn.ylz.ylmall.content.controller;

import cn.ylz.ylmall.account.pojo.vo.PageData;
import cn.ylz.ylmall.account.security.CurrentPrincipal;
import cn.ylz.ylmall.common.web.JsonResult;
import cn.ylz.ylmall.content.pojo.param.CommentAddNewParam;
import cn.ylz.ylmall.content.pojo.vo.CommentListItemVO;
import cn.ylz.ylmall.content.service.ICommentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/comments")
@Validated
@Api(tags = "2.2. 评论模块")
/**
 * 评论,点赞收藏
 */
public class CommentController {

    @Autowired
    private ICommentService commentService;
    public CommentController() {
        log.debug("创建控制器类对象：CommentController");
    }

    /**
     * 发表评论
     * @param currentPrincipal
     * @param commentAddNewParam
     * @return
     */
    @PostMapping("/post-comment")
    @ApiOperation("发表评论")
    @ApiOperationSupport(order = 201)

    public JsonResult PostComment(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @Valid CommentAddNewParam commentAddNewParam){
        log.debug("开始处理【发表评论】的请求，参数：{}", commentAddNewParam);
        commentService.addNew(commentAddNewParam,currentPrincipal);
        return JsonResult.ok("发布成功");
    }

    @PostMapping("/{id:[0-9]+}/post-comment")
    @ApiOperation("发表子级评论")
    @ApiOperationSupport(order = 206)
    public JsonResult PostChildComment(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                  @PathVariable @Range(min = 0,message = "请提交有效的文章ID值！") Long id,
                                  @Valid CommentAddNewParam commentAddNewParam){
        log.debug("开始处理【发表子级评论】的请求，参数：{},父级id:", commentAddNewParam,id);
        commentService.addNew(commentAddNewParam,currentPrincipal,id);
        return JsonResult.ok("发布成功");
    }

    /**
     * 根据评论id删除评论
     * @param id
     * @return
     */
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除评论")
  //  @PreAuthorize("hasAuthority('/comment/comment-delete')")
    @ApiOperationSupport(order = 202)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "评论id", required = true, paramType = "path")
    })
    public JsonResult deleteComment(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                                    @PathVariable @Range(min = 1,message = "请提交有效的文章ID值！") Long id){
        log.debug("开始处理【删除评论】的请求，参数：{}", id);
        commentService.delete(id,currentPrincipal);
        return JsonResult.ok("删除成功");
    }

    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("查询评论")
  //  @PreAuthorize("hasAuthority('/comment/comment-select')")
    @ApiOperationSupport(order = 203)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "评论id", required = true, paramType = "path")
    })
    public JsonResult selectComment(@PathVariable @Range(min = 1,message = "请提交有效的文章ID值！") Long id){
        log.debug("开始处理【查询评论】的请求，参数：{}", id);
        return JsonResult.ok(commentService.getStandardById(id));
    }

    @GetMapping("/list")
   // @PreAuthorize("hasAuthority('/content/comment/read')")
    @ApiOperation("根据文章查询评论列表")
    @ApiOperationSupport(order = 204)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "文章ID", required = true, paramType = "query", dataType = "long"),
    })
    public JsonResult listByCategoryId(@Range(message = "请提交有效的文章ID值！") Long contentId)
    {
        log.debug("开始处理【根据文章查询评论列表】的请求，文章：{}", contentId);
        List<CommentListItemVO> list = commentService.listByContentId(contentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/count")
    // @PreAuthorize("hasAuthority('/content/comment/read')")
    @ApiOperation("根据文章统计评论数量")
    @ApiOperationSupport(order = 205)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contentId", value = "文章ID", required = true, paramType = "query", dataType = "long"),
    })
    public JsonResult count(@Range(message = "请提交有效的文章ID值！") Long contentId)
    {
        log.debug("开始处理【根据文章统计评论数量】的请求，文章：{}", contentId);
        return JsonResult.ok(commentService.countByContentId(contentId));
    }

    @GetMapping("/{id:[0-9]+}/selectChild")
    @ApiOperation("查询子评论")
    //  @PreAuthorize("hasAuthority('/comment/comment-select')")
    @ApiOperationSupport(order = 206)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论id", required = true, paramType = "path")
    })
    public JsonResult selectChildComment(@PathVariable @Range(min = 1,message = "请提交有效的文章ID值！") Long id){
        log.debug("开始处理【查询评论】的请求，参数：{}", id);
        return JsonResult.ok(commentService.getChild(id));
    }

}
